﻿using System.IO;
using System.Text;
using Best.SmartCms.CodeGeneration.Model;
using Best.SmartCms.CodeGeneration.Template;

namespace Best.SmartCms.CodeGeneration.BuiderFactory
{
    public class ServiceBuilder : CodeGenerationFactory
    {
        public override void BuilderTemplate(CodeGenerationDto dto)
        {
            if (!dto.IsBulderService)
            {
                return;
            }

            var controllerPath = Path.Combine(BasePath, "Best.SmartCms.API/Controllers");
            var servicePath = Path.Combine(BasePath, "Best.SmartCms.Service");
            var daoPath = Path.Combine(BasePath, "Best.SmartCms.Dao");
            if (!Directory.Exists(controllerPath))
            {
                Directory.CreateDirectory(controllerPath);
            }

            if (!Directory.Exists(servicePath))
            {
                Directory.CreateDirectory(servicePath);
            }

            if (!Directory.Exists(daoPath))
            {
                Directory.CreateDirectory(daoPath);
            }

            var controllerName= Path.Combine(controllerPath, $"{dto.DomainName}Controller.cs");
            var serviceName= Path.Combine(servicePath, $"{dto.DomainName}Service.cs");
            var daoName= Path.Combine(daoPath, $"{dto.DomainName}Dao.cs");

            var controller = new ControllerTempate(dto);
            var content = controller.TransformText();
            File.WriteAllText(controllerName, content, Encoding.UTF8);

            var service = new ServiceTempate(dto);
            var serviceContent = service.TransformText();
            File.WriteAllText(serviceName, serviceContent, Encoding.UTF8);


            var dao = new DaoTempate(dto);
            var daoContent = dao.TransformText();
            File.WriteAllText(daoName, daoContent, Encoding.UTF8);
        }
    }
}
